﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using System.Reflection;
using DevExpress.XtraEditors;

namespace QL_Truong_Hoc.HocTap
{
    public partial class frmChonHsThiHetMon : DevExpress.XtraEditors.XtraForm
    {
        private int mintMonID;
        private int mintLopID;
        private int mintKdtID;
        private string mstrTenMon;
        private string mstrTenLop;
        private clsEnum.LAN_THI memLanThi;
        private List<int> mlstExceptID;

        public List<int> DanhSachChon { get; set; }

        public frmChonHsThiHetMon(int intMonID, int intLopID, int intKdtID, List<int> lstExceptID, string strTenMon, string strTenLop, clsEnum.LAN_THI emLanThi)
        {
            InitializeComponent();

            mstrTenMon = strTenMon;
            mstrTenLop = strTenLop;
            memLanThi = emLanThi;
            mintMonID = intMonID;
            mintLopID = intLopID;
            mintKdtID = intKdtID;
            mlstExceptID = lstExceptID;
            this.DanhSachChon = new List<int>();
        }

        private void frmChonHsThiHetMon_Load(object sender, EventArgs e)
        {
            try
            {
                this.Text = string.Format("Chọn học sinh lớp {0} thi hết môn {1} lần {2}", mstrTenLop, mstrTenMon, (int)memLanThi + 1);
                string strFilter = "";

                strFilter = "HS_ID not in (";
                for (int i = 0; i <= mlstExceptID.Count - 1; i++)
                {
                    strFilter += string.Format("{0}, ", mlstExceptID[i]);
                }
                strFilter += "-1)";

                this.tHocSinhMonGetFullBindingSource.Filter = strFilter;
                this.tHocSinhMon_GetFullTableAdapter.Fill(this.qLTHDataSet.tHocSinhMon_GetFull, mintKdtID, mintLopID, mintMonID);
                this.qLTHDataSet.tHocSinhMon_GetFull.Columns.Add("colChon", typeof(bool));
                dgvChonHocSinh.BestFitColumns();
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void rdChon_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (rdChon.Checked)
                {
                    for (int i = 0; i <= dgvChonHocSinh.RowCount - 1; i++)
                    {
                        dgvChonHocSinh.SetRowCellValue(i, colChon, true);
                    }

                    rdChon.Text = "Bỏ chọn tất cả";
                }
                else
                {
                    for (int i = 0; i <= dgvChonHocSinh.RowCount - 1; i++)
                    {
                        dgvChonHocSinh.SetRowCellValue(i, colChon, false);
                    }

                    rdChon.Text = "Chọn tất cả";
                }
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void btnChoose_Click(object sender, EventArgs e)
        {
            try
            {
                List<int> lstChon = new List<int>();
                bool blnChon;
                int intHsID;

                for (int i = 0; i <= dgvChonHocSinh.RowCount - 1; i++)
                {
                    blnChon = clsCommon.fncCnvNullToBool(dgvChonHocSinh.GetRowCellValue(i, colChon));

                    if (!blnChon) continue;

                    intHsID = clsCommon.fncCnvNullToInt(dgvChonHocSinh.GetRowCellValue(i, colHS_ID));
                    lstChon.Add(intHsID);
                }

                this.DanhSachChon = lstChon;
                this.Close();
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            try
            {
                this.Close();
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }
    }
}